package com.etone.android.test.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.openqa.selenium.android.AndroidDriver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SeleniumTestUtils {
	private AndroidDriver driver;

	private String filePath;

	public SeleniumTestUtils(AndroidDriver driver, String filePath) {
		super();
		this.driver = driver;
		this.filePath = filePath;
	}

	public void parse() {
		DocumentBuilderFactory factory = null;
		DocumentBuilder builder = null;
		Document document = null;
		InputStream is = null;

		factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			is = new FileInputStream(filePath);
			document = builder.parse(is);

			Element root = document.getDocumentElement();
			if (null != root) {
				NodeList nodeList = root.getChildNodes();
				if (null != nodeList) {
					parseNodeList(nodeList);
				} else {
					throw new RuntimeException("没有找到子结点！");
				}
			} else {
				throw new RuntimeException("没有找到根元素！");
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void parseNodeList(NodeList nodeList) {
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i);
			// 判断node是不是Element
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) node;
			}
		}
	}

}
